#define  _COMMON_FUNC_GLOBAL_
#include "config.h"

static U32 EIC_IRQ_DIScnt = 0;

void EIC_IRQ_DisableINT_All(void)
{
	if (EIC_IRQ_DIScnt == 0) {
		__set_PRIMASK(1);
	} 
	
	EIC_IRQ_DIScnt++;
}

void EIC_IRQ_EnableINT_All(void) 
{
	EIC_IRQ_DIScnt --; 
	if (EIC_IRQ_DIScnt == 0) {
		__set_PRIMASK(0); 
	}
}
U32 forU8ToU32LSB(U8 *p)
{
	U32 iTemp = 0;
	iTemp = (p[3] << 24) + (p[2] << 16) + (p[1] << 8) + p[0];
	return iTemp;
}
U32 forU8ToU32MSB(U8 *p)
{
	U32 tmp;
	tmp = (p[0] << 24) + (p[1] << 16) + (p[2] << 8) + p[3];
	return tmp;
}
void forU32TopU8LSB(U32 data,U8 *p)
{
	p[0] = data;
	data = data >> 8;p[1] = data;
	data = data >> 8;p[2] = data;
	data = data >> 8;p[3] = data;
}

void forU32TopU8MSB(U32 data,U8 *p)
{
	p[0] = data>>24;
	p[1] = data>>16;
	p[2] = data>>8;
	p[3] = data;
}

UINT querryDifferenceSize(UINT a,UINT b)
{
	if (a >= b)
	{
		return a-b;
	}
	else
	{
		return b-a;
	}
}

U32 querryMin(U32 a,U32 b)
{
	if (a > b)
	{
		return b;
	}
	else
	{
		return a;
	}
}

U32 querryMax(U32 a,U32 b)
{
	if (a > b)
	{
		return a;
	}
	else
	{
		return b;
	}
}

U8* addAsciiToUnicode(U8 *p,U8 ascii)
{
	*p++ = 0;
	*p++ = ascii;
	return p;
}

U8* addStringToUnicode(U8 *p,U8 *str)
{
	while (*str)
	{
		p = addAsciiToUnicode(p,*str);
		str++;
	}
	return p;
}

U8* addUnicodeToUnicode(U8 *p,U16 unicode)
{
	*p++ = unicode >> 8;
	*p++ = unicode;
	return p;
}

U8* addUnicodesToUnicode(U8 *p,const U16* unicode)
{
	U16 i = 0;
	while (unicode[i])
	{
		p = addUnicodeToUnicode(p,unicode[i]);
		i++;
	}
	return p;
}

U8* addNumberToUnicode(U8 *p,U32 data,U8 selLen)
{
	U8 buff[16];
	U8 i,len;

	memset(buff,0,sizeof(buff));
	len = 0;
	
	do {
		buff[len++] = data%10;
		data /= 10;
	} while (data);


	if (selLen)
	{
		len = selLen;
	}
	
	for (i = 0;i < len;i++)
	{
		p = addAsciiToUnicode(p,buff[len-i-1]+'0');
	}
	return p;
}

//////////////////////////////////////////////JList8

U8* JListInnerGetU16(U8 *p,U16 *pNumber)
{
	*pNumber = (p[1] << 8) + p[0];
	return &p[2];
}

BOOL JList8HaveSame(U8 *p,U16 buffSize,U8 *pItem,U8 size)
{
	U8 length;

	do {
		length = *p;
		p++;
		if (length)
		{
			if (0 == memcmp(p,pItem,length))
			{
				return TRUE;
			}
		}
		p += length;
	} while (length);

	return FALSE;
}

U8* JList8InnerGetTail(U8 *p)
{
	U8 length;

	do {
		length = *p;
		p++;
		if (length)
		{
			p += length;
		}
	} while (length);

	p--;//with ++ in while
	return p;
}

U16 JList8AddOne(U8 *p,U16 buffSize,U8 *pItem,U8 size)
{
	U8 *pSrc = p;
	if (!JList8HaveSame(p,buffSize,pItem,size))
	{
		p = JList8InnerGetTail(p);

		if (p - pSrc + 1 + size + 1 < buffSize)
		{
			*p = size;
			p++;
			memcpy(p,pItem,size);
			p += size;
		}
		else
		{
			return FALSE;
		}
	}
	return p-pSrc;
}

U8* JList8GetOne(U8 *p,U16 buffSize,U16 offset,U8* length)
{
	U8 retLength;
	U16 now;

	now = 0;
	do {
		retLength = *p;
		p++;
		if (retLength && now == offset)
		{
			*length = retLength;
			return p;
		}
		p += retLength;
		now++;
	} while (retLength);

	return NULL;
}

U16 JList8GetCount(U8 *p,U16 buffSize)
{
	U8 length;
	U16 i,retCount;

	retCount = 0;
	for (i = 0;i < buffSize;i++)
	{
		length = *p;
		p++;
		if (0 == length)
		{
			return retCount;
		}
		else
		{
			retCount++;
			p += length;
		}
	}

	return retCount;
}

U16 JList8GetSize(U8 *p,U16 buffSize)
{
	U8 *pStart = p;
	U8 length;
	U16 i;

	for (i = 0;i < buffSize;i++)
	{
		length = *p;
		p++;
		if (0 == length)
		{
			return p - pStart;
		}
		else
		{
			p += length;
		}
	}

	return p - pStart;
}

void JList8Init(U8 *p,U16 buffSize)
{
	memset(p,0,buffSize);
}

///////////////////////////////////////UTF-8 to Unicode

U16 addToUnicode(U16 unicode,U8 *srcBuff,U16 objLen)
{
	if (objLen >= 2)
	{
		*srcBuff++ = unicode;
		*srcBuff++ = unicode >> 8;
		return 2;
	}
	else
	{
		return 0;
	}
}

U16 UTF8toUnicode(U8 *srcBuff,U16 srcLen,U8 *objBuff,U16 objLen)
{
	U16 i;
	U8 *srcP = srcBuff;
	U8 *objP = objBuff;
	U16 tempUnicode;
	U16 returnLen = 0;
	
	for (i = 0;i < srcLen;)
	{
		if (*srcP <= 0x7F)
		{
			tempUnicode = *srcP;
			returnLen += addToUnicode(tempUnicode,objP,objLen);
			srcP++;
			i++;
			objP+=2;
			objLen -= 2;
		}
		else if ((*srcP & 0xE0) == 0xC0)
		{
			tempUnicode = ((*srcP&0x1F)<<6)|(*(srcP+1)&0x3F);
			returnLen += addToUnicode(tempUnicode,objP,objLen);
			srcP+=2;
			i+=2;
			objP+=2;
			objLen -= 2;
		}
		else if ((*srcP & 0xF0) == 0xE0)
		{
			tempUnicode = ((*srcP&0x0F)<<12)|((*(srcP+1)&0x3F)<<6)|(*(srcP+2)&0x3F);
			returnLen += addToUnicode(tempUnicode,objP,objLen);
			srcP+=3;
			i+=3;
			objP+=2;
			objLen -= 2;
		}
	}

	return returnLen;
}

